package coml.jiyun.hp.zuoye.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.zuoye.R;
import coml.jiyun.hp.zuoye.bean.WanBean;

public class RlvHomeAdapter extends RecyclerView.Adapter<RlvHomeAdapter.ViewHolder> {
    public OnLongClickListener onLongClickListener;

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }
   public interface OnLongClickListener{
        void OnLongClick(int position);
    }

    private Context context;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;

    public RlvHomeAdapter(Context context, ArrayList<WanBean.DataBean.DatasBean> datasBeans) {
        this.context = context;
        this.datasBeans = datasBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_item1, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(datasBeans.get(position).getEnvelopePic()).apply(new RequestOptions().circleCrop()).into(holder.mImg);
        holder.mTv.setText(datasBeans.get(position).getTitle());
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onLongClickListener!=null){
                    onLongClickListener.OnLongClick(position);
                }
                return true;
            }
        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClick.dj(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datasBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mImg;
        TextView mTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mImg =itemView.findViewById(R.id.img);
            mTv = itemView.findViewById(R.id.tv);
        }
    }
    public interface itemOnClick{
        void dj(int i);
    }

    itemOnClick itemOnClick;

    public void setItemOnClick(RlvHomeAdapter.itemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }
}